package com.yzt.zhuju.network.biz;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.p_base.utils.LogDataUtils;
import com.yzt.plugin_network.response.HttpResponse;
import com.yzt.plugin_network.response.HttpResponsePre;
import com.yzt.plugin_network.response.HttpResponsePre2;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   : 模块基类
 */
public abstract class BaseModule {

    public <T> void toSubscribe(Observable<T> observable, Observer<? super T> observer, String apiName) {
        if (!TextUtils.isEmpty(apiName)) {
            ((AresSubscriber) observer).setApiName(apiName);
        }
        observable.subscribeOn(Schedulers.computation())
                .unsubscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 订阅一个根据泛型泛型过滤返回数据得观察者
     */
    public <T> void toSubscribe2Filter(Observable observable, Observer<? super T> observer, String apiName) {
        if (!TextUtils.isEmpty(apiName)) {
            ((AresSubscriber) observer).setApiName(apiName);
        }

        Type genericSuperclass = observer.getClass().getGenericSuperclass();
        Type type = Object.class;
        if(genericSuperclass instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType)genericSuperclass).getActualTypeArguments();
            type = types[0];
            if(types[0] instanceof ParameterizedType) {
                type = ((ParameterizedType)types[0]).getRawType();
            }
        }

        if(type.toString().contains(HttpResponse.class.getName()) || type.toString().contains(Object.class.getName())) {
            observable = observable.map(new HttpResponsePre2<>());
        } else {
            observable = observable.map(new HttpResponsePre<>());
        }

        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public RequestBody getRequestBody(Object object) {
        MediaType mediaType = MediaType.Companion.parse("application/json;charset=utf-8");
        return RequestBody.Companion.create(JSON.toJSONString(object), mediaType);
    }

    public RequestBody getRequestBody(JSONObject json) {
        LogDataUtils.e("upload parameter: " + JSON.toJSONString(json));
        MediaType mediaType = MediaType.Companion.parse("application/json;charset=utf-8");
        return RequestBody.Companion.create(json.toJSONString(), mediaType);
    }

    public RequestBody getRequestBody(String content) {
        MediaType mediaType = MediaType.Companion.parse("application/json;charset=utf-8");
        return RequestBody.Companion.create(content, mediaType);
    }

}
